package io.renren.modules.sys.lucene.controller;



import io.renren.common.utils.R;

import io.renren.modules.sys.back.service.*;

import io.renren.modules.sys.lucene.service.*;


import io.renren.modules.sys.web_api.service.FirstPageService;
import io.renren.modules.sys.web_api.service.WendaPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author goudandan
 * @email reszj1@gmail.com
 * @date 2018-09-12 16:49:25
 */
@Controller
@RequestMapping("api/sreach")
public class LucenceSreachController {
    @Autowired
    private WendaPageService wendaPageService;
    @Autowired
    private FirstPageService firstPageService;
    @Autowired
    private LuceneServicImpl luceneServicImpl;
    @Autowired
    private LuceneJiShuServicImpl luceneJiShuServicImpl;
    @Autowired
    private LucenePinPaiServicImpl lucenePinPaiServicImpl;
    @Autowired
    private LuceneRenWuServicImpl luceneRenWuServicImpl;
    @Autowired
    private LuceneWenDaServicImpl luceneWenDaServicImpl;

    @RequestMapping("/getdata")
    public String CrawlerFirst(String param,Map<String,Object> backdata){
        List<Map<String,Object>> fenlei = firstPageService.firstPageProFenLei();
        backdata.put("fenlei",fenlei);
        try {
            List<Map<String,Object>> chanpin = luceneServicImpl.findIndexDB(param);
            if(chanpin!=null && chanpin.size()>0){
                backdata.put("chanpin",chanpin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            List<Map<String,Object>> chanpin = luceneJiShuServicImpl.findIndexDB(param);
            if(chanpin!=null && chanpin.size()>0){
                backdata.put("jishu",chanpin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            List<Map<String,Object>> chanpin = luceneRenWuServicImpl.findIndexDB(param);
            if(chanpin!=null && chanpin.size()>0){
                backdata.put("renwu",chanpin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            List<Map<String,Object>> chanpin = lucenePinPaiServicImpl.findIndexDB(param);
            if(chanpin!=null && chanpin.size()>0){

                backdata.put("pinpai",chanpin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            List<Map<String,Object>> chanpin = luceneWenDaServicImpl.findIndexDB(param);
            if(chanpin!=null && chanpin.size()>0){
                backdata.put("wenda1",chanpin);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        backdata.put("param",param);
        List<Map<String,Object>> wenda = wendaPageService.getAllWenda();
        backdata.put("wenda",wenda);
        return "webpage/searchpage";
    }
    @RequestMapping("/creatindex")
    public void CreatIndex(){
        try {
           if(luceneServicImpl.createIndexDB()) {
               System.out.println("产品index！！！！！！！！！！！！！！Ok\n");
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if(luceneJiShuServicImpl.createIndexDB()) {
                System.out.println("技术index！！！！！！！！！！！！！！Ok\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if(lucenePinPaiServicImpl.createIndexDB()) {
                System.out.println("品牌index！！！！！！！！！！！！！！Ok\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if(luceneRenWuServicImpl.createIndexDB()) {
                System.out.println("人物index！！！！！！！！！！！！！！Ok\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            if(luceneWenDaServicImpl.createIndexDB()) {
                System.out.println("问答index！！！！！！！！！！！！！！Ok\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
// 数据抓取
  /*  @Autowired
     private BtCtBaseService btCtBaseService;
    @Autowired
    private BtWendaInfoService btWendaInfoService;
    @ResponseBody
    @RequestMapping("/getimg")
    public R getimg(String account,String pswd){

       *//* List<BtWendaInfoEntity> blist = btWendaInfoService.selectList(new EntityWrapper<BtWendaInfoEntity>());
        for(BtWendaInfoEntity sb: blist){
            String tmp = sb.getContent();
                if(!tmp.equals("")){
                    Document doc = Jsoup.parse(tmp);
                    Elements subimg = doc.getElementsByTag("img");
                    if(subimg!=null&&subimg.size()>0) {
                        sb.setImg(subimg.get(0).attr("src"));
                        btWendaInfoService.insertOrUpdate(sb);
                    }
                }
            }*//*
            *//*BtCtGaishuEntity btCtGaishuEntity = btCtGaishuService.selectById(sb.getGsid());
            if(btCtGaishuEntity !=null){
                *//**//*String tmp = btCtGaishuEntity.getContent();
                if(!tmp.equals("")){
                    Document doc = Jsoup.parse(tmp);
                    Elements subimg = doc.getElementsByTag("img");
                    if(subimg!=null&&subimg.size()>0) {
                        sb.setImg(subimg.get(0).attr("src"));
                        btCtBaseService.insertOrUpdate(sb);
                    }
                }*//**//*
            }*//*
        return R.ok().put("fdsaf","dfdfdsa").put("jjjjj","科技的说法");
        }*/

}
